package com.android.shop.ui;

import java.util.Timer;
import java.util.TimerTask;
import com.android.shop.R;
import com.android.shop.api.MyHttpClient;
import com.android.shop.bean.SellerBean;
import com.android.shop.common.AsyncImageLoad;
import com.android.shop.common.Utils;
import com.android.shop.constant.MyConstantID;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class HomePageActivity extends BaseActivity {
	private LinearLayout orderCheck, addFood, dishEdit, sellerEdit;
	private Button btn_shopstate,login_setting;
	private TextView tv_shop_name;
	private ImageView img_shop;
	SellerBean seller;
	String result;
	boolean isExit=false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home_page);
		initView();
	}

	private void initView() {
		orderCheck = (LinearLayout) this.findViewById(R.id.orderCheck_linear);
		addFood = (LinearLayout) this.findViewById(R.id.addFood_linear);
		dishEdit = (LinearLayout) this.findViewById(R.id.dish_linear);
		sellerEdit = (LinearLayout) this.findViewById(R.id.seller_linear);
		btn_shopstate = (Button) this.findViewById(R.id.btn_shopstate);
		login_setting=(Button) this.findViewById(R.id.login_setting);
		img_shop=(ImageView)this.findViewById(R.id.img_shop);

		orderCheck.setOnClickListener(listener);
		addFood.setOnClickListener(listener);
		dishEdit.setOnClickListener(listener);
		sellerEdit.setOnClickListener(listener);
		btn_shopstate.setOnClickListener(listener);
		login_setting.setOnClickListener(listener);
		
		tv_shop_name=(TextView)this.findViewById(R.id.tv_shop_name);

		new Thread(new ReceiveThread()).start(); // 开启线程获取商家数据
	}

	class ReceiveThread implements Runnable {
		@Override
		public void run() {
			SharedPreferences sp = getSharedPreferences(MyConstantID.SP_NAME,Activity.MODE_PRIVATE);  
			String sellerid= sp.getString("sellerid", "");//获取商户id
			seller = MyHttpClient.getSingle().getSeller(sellerid);
			handler.sendEmptyMessage(11);
		}
	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 11) {
				if (seller != null) {
					String state = seller.getState();
					if(Utils.isNullOrEmpty(state)){
						if (state.equals("0"))
						{
							btn_shopstate.setText("打烊中");
							btn_shopstate.setTextColor(Color.RED);
						}
						else if (state.equals("1"))
						{
							btn_shopstate.setText("营业中");
							btn_shopstate.setTextColor(Color.GREEN);
						}
					}
					tv_shop_name.setText(seller.getShopname());
					new AsyncImageLoad(getBaseContext(),img_shop , MyConstantID.IMAGE_PATH+seller.getShopphoto());
				} else {
					Toast.makeText(HomePageActivity.this, "网络问题,请稍后再试",
							Toast.LENGTH_LONG).show();
				}
			}
		}
	};

	class SubmitThread implements Runnable {
		@Override
		public void run() {
			SharedPreferences sp = getSharedPreferences(MyConstantID.SP_NAME,Activity.MODE_PRIVATE);  
			String sellerid= sp.getString("sellerid", "");//获取商户id
			result = MyHttpClient.getSingle().modifySellerState(sellerid);
			handlerSubmit.sendEmptyMessage(11);
		}
	}

	Handler handlerSubmit = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 11) {
				if (result.equals("SUC")) {
					new Thread(new ReceiveThread()).start(); // 开启线程获取商家数据
				} else {
					Toast.makeText(getApplicationContext(), "网络异常",
							Toast.LENGTH_SHORT).show();
				}
			}
		}
	};

	View.OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.orderCheck_linear:
				Intent intent = new Intent();
				intent.setClass(HomePageActivity.this, OrderListActivity.class);
				startActivity(intent);
				break;
			case R.id.addFood_linear:
				Intent intentAddFood = new Intent();
				intentAddFood.setClass(HomePageActivity.this,
						AddFoodActivity.class);
				startActivity(intentAddFood);
				break;
			case R.id.dish_linear:
				Intent intentdish = new Intent();
				intentdish.setClass(HomePageActivity.this,
						DishListTabActivity.class);
				startActivity(intentdish);
				break;
			case R.id.seller_linear:
				Intent intentseller = new Intent();
				intentseller.setClass(HomePageActivity.this,
						SellerActivity.class);
				startActivity(intentseller);
				break;
			case R.id.btn_shopstate:
				new Thread(new SubmitThread()).start();		
				break;
			case R.id.login_setting:
				Intent intentsetting = new Intent();
				intentsetting.setClass(HomePageActivity.this,
						SettingActivity.class);
				startActivity(intentsetting);
				break;
			default:
				break;
			}
		}
	};

	@Override
	public void finish() {
		if (isExit == false) {
			isExit = true;
			Toast.makeText(this, "再按一次返回键退出", Toast.LENGTH_SHORT).show();
			new Timer().schedule(new TimerTask() {
				@Override
				public void run() {
					isExit = false;
				}
			}, 2500);
		} else {
			HomePageActivity.super.finish();
		}
	}
}
